﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZhimeiSoft.Framework.Client.Utils;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.Framework.Common.Enums;

namespace ZhimeiSoft.CharmManager.Client.Forms.Control.ServiceItem
{
    public partial class CustomerServiceItemControl : UserControl
    {
        private int iCustomerID = -1;
        private DataSet dsCustomerServiceItem;

        public CustomerServiceItemControl()
        {
            InitializeComponent();
            customSearch1.onSerchCustom = this.SerchCustomInfo;
        }

        /// <summary>
        /// 搜索会员控件返回的会员信息
        /// </summary>
        /// <param name="drCustomInfo"></param>
        private void SerchCustomInfo(DataRow drCustomInfo)
        {
            if (drCustomInfo == null)
            {
                return;
            }

            iCustomerID = Convert.ToInt32(drCustomInfo[DBEntityCustomInfo.FieldName.CUSTOMID]);

            this.lblName.Text = drCustomInfo[DBEntityCustomInfo.FieldName.CNAME].ToString();
            this.lblNickName.Text = drCustomInfo[DBEntityCustomInfo.FieldName.CNICKNAME].ToString();
            this.lblPhone.Text = drCustomInfo[DBEntityCustomInfo.FieldName.CMOBILEPHONE].ToString();
            this.lblLastAccessDate.Text = drCustomInfo[DBEntityCustomInfo.FieldName.CLASTREGISTERDATE].ToString();

            InitCustomerServiceItem(iCustomerID);
        }

        private void InitCustomerServiceItem(int customerID)
        {
            DBEntityCustomerServiceItem item = new DBEntityCustomerServiceItem();
            string error = string.Empty;
            string sFilter = string.Format("{0}.{1}={2}", item.GetTableName(), DBEntityCustomerServiceItem.FieldName.CUSTOMERID, customerID.ToString());
            dsCustomerServiceItem = new DataSet();
            bool bFlag = BussinessProxyManager.Instance.QueryCustomerServiceItem(sFilter, out dsCustomerServiceItem, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo(error);
                return;
            }
            gdServiceItem.DataSource = dsCustomerServiceItem.Tables[0].DefaultView;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (iCustomerID == -1)
            {
                MsgBox.MsgInfo("请先选择会员。");
                return;
            }
            frmCustomerServiceItemAdd form = new frmCustomerServiceItemAdd();
            
            if (form.ShowDialog() == DialogResult.OK)
            {
                InitCustomerServiceItem(iCustomerID);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int iRowHandle = gvServiceItem.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择要删除的服务项目。");
                return;
            }
            DataRow dr = gvServiceItem.GetDataRow(iRowHandle);
            int iCustomerServceItemID = Convert.ToInt32(dr[DBEntityCustomerServiceItem.FieldName.CUSTOMERSERVICEITEMID]);
            string error = string.Empty;
            bool bFlag = BussinessProxyManager.Instance.DeleteCustomerServiceItem(iCustomerServceItemID, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("删除会员服务项目失败，" + error);
                return;
            }
            else
            {
                InitCustomerServiceItem(iCustomerID);
            }
        }

        private void btnSetPlan_Click(object sender, EventArgs e)
        {
            int iRowHandle = gvServiceItem.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo("请先选择服务项目。");
                return;
            }


        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int iRowHandle = gvServiceItem.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择要修改的服务项目。");
                return;
            }

            DataRow dr = gvServiceItem.GetDataRow(iRowHandle);
            DBEntityCustomerServiceItem item = new DBEntityCustomerServiceItem(dr);

            frmCustomerServiceItemAdd form = new frmCustomerServiceItemAdd();
            form.Mode = ControlClassModeEnum.emEdit;
            if (form.ShowDialog() == DialogResult.OK)
            {
                InitCustomerServiceItem(iCustomerID);
            }
        }
    }
}
